package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.WithdrawalCleanMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.service.WithdrawalCleanService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 保洁房源
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-31 16:26
 **/
@Service
public class WithdrawalCleanServiceImpl implements WithdrawalCleanService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(WithdrawalCleanServiceImpl.class);

    @Autowired
    WithdrawalCleanMapper withdrawalCleanMapper;

    @Autowired
    ZpcMktFeign zpcMktFeign;

    @Autowired
    ZzuulFeign zzuulFeign;

    /**
    * <p>
    *  查询保洁房源胡数量
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public Integer getIfCleanFeeCount(Map<String,Object> params){

        System.out.println("传进来的参数"+params);
        JsonResult zpcMktResult = zpcMktFeign.getCleanHousingList(params);
        System.out.println("去市场部查询的结果"+zpcMktResult);
        List<String> HouInfoIdList = new ArrayList<>();
        List<Map<String,Object>> zpcMktList = (List<Map<String, Object>>) zpcMktResult.get("result");
        for (Map<String,Object> zpcMktMap:zpcMktList) {
            if(zpcMktMap.get("HouInfoId")!=null){
                HouInfoIdList.add(MapUtils.getString(zpcMktMap,"HouInfoId"));
            }
        }
        System.out.println("符合省市区的房源集合"+HouInfoIdList);

        if (HouInfoIdList.size()>0){
            params.put("HouInfoIdList",HouInfoIdList);
        }
        if(HouInfoIdList.size()<=0){
            HouInfoIdList.add("0");
            params.put("HouInfoIdList",HouInfoIdList);
        }
        System.out.println("加入省市区和发起人的参数"+params);
        Integer cout = withdrawalCleanMapper.getIfCleanFeeCount(params);
        return cout;
    }

    /**
    * <p>
    * 保洁房源的集合
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public List<Map<String,Object>> getIfCleanFeeList(Map<String,Object> params) {
        System.out.println("传进来的参数"+params);
        JsonResult zpcMktResult = zpcMktFeign.getCleanHousingList(params);
        System.out.println("去市场部查询的结果"+zpcMktResult);
        List<String> HouInfoIdList = new ArrayList<>();
        List<Map<String,Object>> zpcMktList = (List<Map<String, Object>>) zpcMktResult.get("result");
        for (Map<String,Object> zpcMktMap:zpcMktList) {
            if(zpcMktMap.get("HouInfoId")!=null){
                HouInfoIdList.add(MapUtils.getString(zpcMktMap,"HouInfoId"));
            }
        }
        System.out.println("符合省市区的房源集合"+HouInfoIdList);

        if (HouInfoIdList.size()>0){
            params.put("HouInfoIdList",HouInfoIdList);
        }
        if(HouInfoIdList.size()<=0){
            HouInfoIdList.add("0");
            params.put("HouInfoIdList",HouInfoIdList);
        }
        System.out.println("加入省市区和发起人的参数"+params);
        List<Map<String,Object>> getIfCleanFeeList = withdrawalCleanMapper.getIfCleanFeeList(params);

        for (Map<String,Object> getIfCleanFeeMap:getIfCleanFeeList) {
            if (getIfCleanFeeMap.get("HouInfoId") !=null){
                Map<String,Object> map1 = new HashMap<>();
                map1.put("HouInfoId",getIfCleanFeeMap.get("HouInfoId"));
                JsonResult zpcMktResultOne = zpcMktFeign.getCleanHousingList(map1);
                List<Map<String,Object>> zpcMktListOne =(List<Map<String, Object>>) zpcMktResultOne.get("result");
                for (Map<String,Object> zpcMktMapOne:zpcMktListOne) {
                    if (MapUtils.getString(zpcMktMapOne,"HouInfoId").equals(MapUtils.getString(getIfCleanFeeMap,"HouInfoId"))){
                        getIfCleanFeeMap.putAll(zpcMktMapOne);
                    }
                }
            }
            if (getIfCleanFeeMap.get("LaunchId")!=null){
                getIfCleanFeeMap.put("user_id",getIfCleanFeeMap.get("LaunchId"));
                JsonResult zzulResult = zzuulFeign.getSystemUser(getIfCleanFeeMap);
                Map<String,Object> zzulMap = (Map<String, Object>) zzulResult.get("result");
                if (MapUtils.getString(zzulMap,"user_id").equals(MapUtils.getString(getIfCleanFeeMap,"user_id"))){
                    getIfCleanFeeMap.putAll(zzulMap);
                }
            }
        }
        return getIfCleanFeeList;
    }




    /**
    * <p>
    * 修改保洁房源保洁状态
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public JsonResult updateIfCleanFee(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("更新操作");
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term","4");//1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            System.out.println("params"+params);
            if(withdrawalCleanMapper.updateIfCleanFee(params)<=0){
                result.putFailed("修改失败!");
            }else{
                result.putSuccess("修改成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;

    }

}
